<?php

namespace App\Console\Commands\Shop;

use App\Enums\Common\NsqEnum;
use App\Remote\Rms\RmsShopRemote;
use App\Service\OpenShop\FeishuMini\ShopService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Contract\Message;

class NsqSubShopAttributeCommand extends Command
{
    protected $signature = 'shop:nsq-sub-shop-attribute-event';

    protected $description = 'nsq监听门店属性事件通知';

    public function handle()
    {
        ini_set('memory_limit', '2G');

        (new NsqSubscriber(config('nsq.default.address')))
            ->sub(
                NsqEnum::TOPIC_RMS_SHOP_ATTRIBUTE_EVENT, NsqEnum::CHANNEL_TIGER_SHOP_ATTRIBUTE_EVENT,
                function (Message $message) {
                    $data = $message->data();

                    if (isTest()) {
                        dump($data);
                    }

                    // 执行
                    $this->process($data);

                    $message->done();
                }
            );
    }


    /**
     * 门店属性修改数据同步 根据对应字段更新到不同表中
     * @param array $data
     */
    private function process(array $data): void
    {
        Log::info('shop-attribute-event', ['data' => $data]);

//        $shopAttributesData = RmsShopRemote::getShopAttribute($data['shopId']) ?? [];// 根据门店id查询最新的门店属性数据

        ShopService::updateShopAttribute($data['shopId'], $data['data']);//　rms门店属性修改同步到选址表单中

    }
}
